import {getToken, setToken, removeToken, setMsgKey} from '@/utils/auth'
import {login, getInfo, logout, giteeLogin} from '@/api/user'
import {Message} from 'element-ui';
import {getUnreadList} from "@/api/userMessage";

const getDefaultState = () => {
    return {
        token: getToken(),
        id: '',
        name: '',
        nickname: '',
        avatar: '',
        type: '',
        email: '',
        userInfo: {},
        msgIp: ''
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_ID: (state, id) => {
        state.id = id
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_NICKNAME: (state, nickname) => {
        state.nickname = nickname
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    SET_USER_INFO(state, userInfo) {
        state.userInfo = userInfo
    },
    SET_MSG_IP(state, ip) {
        state.msgIp = ip
    }
}

const actions = {
    goLogin(context, {data, type}) {
        return new Promise((resolve, reject) => {
            let result = new Promise((resolve, reject) => {
                if (type == 'system') {
                    data.grant_type = 'password'
                    login(data).then(res => {
                        resolve(res)
                        // const {access_token, token_type} = res.data
                        // const token = token_type + " " + access_token
                        // context.commit('SET_TOKEN', token)
                        // setToken(token)
                        // resolve(res)
                    })
                } else if (type == 'gitee') {
                    giteeLogin(data).then(res => {
                        resolve(res)
                    })
                }
            })
            result.then(res => {
                const {code, msg, status} = res
                if (status) {
                    const {access_token, token_type} = res.data;
                    const token = token_type + " " + access_token;
                    context.commit('SET_TOKEN', token);
                    setToken(token);
                }
                resolve(res);


            })
        })
    },

    getUserInfo({commit, state}) {
        return new Promise((resolve, reject) => {
            getInfo(state.token).then(res => {
                const userInfo = res.data
                commit('SET_NAME', userInfo.username)
                commit('SET_ID', userInfo.id)
                commit('SET_NICKNAME', userInfo.nickname)
                commit('SET_AVATAR', userInfo.avatar)
                commit('SET_USER_INFO', userInfo)
                getUnreadList().then(res => {
                    if (res.status) {
                        let unreadList = res.data
                        if (unreadList.length > 0 && unreadList) {
                            commit("SET_MESSAGE", unreadList)
                        }
                    }
                })
                resolve(userInfo)
            }).catch(err => {
                reject(err)
            })
        })
    },

    logout({commit}) {
        return new Promise(resolve => {
            logout().then(() => {
                removeToken()
                commit('SET_TOKEN', '');
                commit('RESET_STATE')
                Message.warning('注销成功')
            })
            resolve()
        })
    },

    setMsgKey({commit}, ip) {
        setMsgKey(ip)
        commit('SET_MSG_IP', ip)
    }
}

export default {
    state,
    mutations,
    actions,
}
